#!/usr/bin/python

# Cheng-Chun Tu
# Dec 27, 2012

"""
This example creates a multi-controller network from
semi-scratch; note a topo object could also be used and
would be passed into the Mininet() constructor.
"""

from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.link import Intf

Switch = OVSKernelSwitch

def addHost( net, N ):
	"Create host hN and add to net."
	name = 'h%d' % N
	ip = '10.0.0.%d' % N
	mac = '00:00:00:00:00:%d' % N    
	return net.addHost( name, ip=ip, mac=mac )

def addSwitch( net, name):
	return net.addSwitch(name)

def TopologyHotnet():
	"Create a network with multiple controllers."

	net = Mininet( controller=RemoteController, switch=Switch)
	print "*** Creating controllers"
	c1 = net.addController('c1', port=6633, ip='127.0.0.1')

	print c1.IP 

	print "*** Creating switches"
	sw1 = addSwitch(net, 's1')
	sw2 = addSwitch(net, 's2')
	sw1.config(mac='00:00:00:00:01:00', ip='10.1.0.1')
	sw2.config(mac='00:00:00:00:02:00', ip='10.1.0.2')

#intf1 = Intf(name='myeth1')

	print "*** Creating hosts"
	h1 = addHost(net, 1)
	h2 = addHost(net, 2)
	h3 = addHost(net, 3)
    
	print "*** Creating links"
	net.addLink(h1, sw1)
	net.addLink(h1, sw2)
	net.addLink(h2, sw1)
	net.addLink(h3, sw2)
	net.addLink(sw1, sw2)

	print "connection information"
	print h1.intfList()
	n = 0
	for intfname in h1.intfList():
		print "setting up interface:", intfname
		h1.setMAC('00:00:00:00:01:%d' % n, intfname)
		n = n + 1
	
	for intfname in sw1.intfList():
		print "setting up interface:", intfname
		sw1.setMAC('00:00:00:00:F1:%d' % n, intfname)
		n = n + 1
	
	print "*** show connection"
	print "how h1 connect to sw1:", h1.connectionsTo(sw1)
	print "how h1 connect to sw2:", h1.connectionsTo(sw2)

	print "*** Starting network"
	net.build()
#	c1.start()
	sw1.start( [ c1 ] )
	sw2.start( [ c1 ] )

#	print "*** Testing network"
#	net.pingAll()

	print "*** Running CLI"
	CLI( net )

	print "*** Stopping network"
	net.stop()

if __name__ == '__main__':
	setLogLevel( 'info' )  # for CLI output
	TopologyHotnet()
